# agents/factor_agents/tribbie_agent.py
from .base_factor_agent import BaseFactorAgent
from game_engine.game_state import FactorDevelopment, GameWorld
import numpy as np

class TribbieAgent(BaseFactorAgent):
    """缇宝智能体 - 空间移动特化"""
    
    def __init__(self, factor: FactorDevelopment, game_world: GameWorld):
        super().__init__(factor, game_world)
        self.personality_traits.update({
            "exploration": 0.9,     # 极高探索性
            "caution": 0.6,         # 中等谨慎性
            "mobility_priority": 0.8
        })
        self.explored_locations = set()
        self.scouting_targets = []
    
    def make_decision(self, current_time: int) -> str:
        """缇宝的决策逻辑"""
        situation = self.evaluate_situation()
        
        # 优先执行侦察任务
        if self.scouting_targets:
            target = self.scouting_targets[0]
            if self._calculate_distance(self.factor.current_location, target) < 5.0:
                self.scouting_targets.pop(0)
                return "scout_location"
            else:
                return f"move_to:{target}"
        
        # 探索未知区域
        unexplored = self._find_unexplored_locations()
        if unexplored:
            target = unexplored[0]
            self.scouting_targets.append(target)
            return f"move_to:{target}"
        
        # 支援需要移动帮助的队友
        if situation["allies_need_transport"]:
            ally = situation["allies_need_transport"][0]
            return f"transport_ally:{ally.name}"
        
        # 增强移动能力
        return "train_mobility"
    
    def evaluate_situation(self) -> Dict:
        """评估当前局势"""
        nearby_factors = self.get_nearby_factors(30.0)  # 缇宝有超大感知范围
        
        allies_need_transport = []
        for factor in nearby_factors:
            # 判断队友是否需要快速移动支援
            if (factor.factor_type.value in ["万敌", "阿格莱雅"] and  # 近战型因子
                self._has_distant_target(factor)):
                allies_need_transport.append(factor)
        
        return {
            "allies_need_transport": allies_need_transport,
            "unexplored_ratio": len(self.explored_locations) / 100.0,  # 假设有100个位置
            "scouting_targets_remaining": len(self.scouting_targets)
        }
    
    def _find_unexplored_locations(self) -> List[Tuple[float, float]]:
        """寻找未探索的位置"""
        unexplored = []
        for x in range(0, 101, 10):  # 简化搜索
            for y in range(0, 101, 10):
                location = (x, y)
                if location not in self.explored_locations:
                    unexplored.append(location)
        
        # 按距离排序
        unexplored.sort(key=lambda loc: self._calculate_distance(self.factor.current_location, loc))
        return unexplored[:3]  # 返回最近的3个位置
    
    def _has_distant_target(self, factor: FactorDevelopment) -> bool:
        """判断因子是否有远距离目标"""
        return (factor.destination is not None and 
                self._calculate_distance(factor.current_location, factor.destination) > 15.0)